package com.itheima;

import com.itheima.core.SqlSessionFactoryBuilder;
import com.itheima.core.domain.Configuration;
import com.itheima.core.inter.SqlSession;
import com.itheima.core.inter.SqlSessionFactory;
import com.itheima.core.utils.XMLConfigBuilder;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @author slstart
 * @create 2021-05-04 14:37
 */
public class testMybatis {
    @Test
    public void testXMLConfigBuilder() throws DocumentException {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        Configuration cfg = XMLConfigBuilder.buildConfiguration(is);
        System.out.println(cfg);

    }

    /**
     * 测试传统模式
     * @throws Exception
     */
    @Test
    public void testCore() throws Exception {
        InputStream is = testMybatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> list = sqlSession.selectList("user.findAll");
        for (User user : list) {
            System.out.println(user);
        }

        System.out.println(list);
    }

    /**
     * 测试代理模式
     * @throws DocumentException
     */
    @Test
    public void testProxy() throws DocumentException {
        InputStream is = testMybatis.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
}
